package com.followme.module.common;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: Kamil.O
 * Description: Adapter Spinner
 * Date: 08.12.13
 * Time: 21:17
 */
public class KeyValueSpinner extends ArrayAdapter<String> {
    protected Context context;
    protected ArrayList<SpinnerContent> spinnerContents;

    public KeyValueSpinner(Context context, int textViewResourceId,
                           String[] objects) {

        super(context, textViewResourceId, objects);
    }

    @Override
    public int getCount() { return spinnerContents.size(); }

    @Override
    public int getViewTypeCount() { return android.R.layout.simple_spinner_item; }

    @Override
    public View getDropDownView(int position, View convertView,
                                ViewGroup parent) {

        LayoutInflater inflater =
                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        TextView textview =
                (TextView) inflater.inflate(android.R.layout.simple_spinner_item, null);
        textview.setText(spinnerContents.get(position).getName());

        return textview;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater =
                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        TextView textview =
                (TextView) inflater.inflate(android.R.layout.simple_spinner_item, null);
        textview.setText(spinnerContents.get(position).getName());

        return textview;
    }

    public int getIdFromIndex(int Index) { return spinnerContents.get(Index).getId(); }

    public int getIndexByID(int id) {
        for(int i=0; i<spinnerContents.size(); i++){
            if (spinnerContents.get(i).getId() == id){
                return i;
            }
        }

        return -1;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {}

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {}

    @Override
    public long getItemId(int position) { return 0; }

    @Override
    public boolean hasStableIds() { return false; }

    @Override
    public int getItemViewType(int position) { return 0; }

    @Override
    public boolean isEmpty() { return false; }
}
